﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;

using System.Threading.Tasks;
using System.Windows.Forms;


namespace DemoForHBQ
{
    public partial class FrmMain : Form
    {
        //对于Timer,定义全局变量
        public int currentCount = 0;

        //游戏对象说明
        private Game _game;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            ///初始化游戏类
            _game = new Game();

            //设置Timer控件可用
            this.timerForGame.Enabled = true;
            //设置时间间隔（毫秒为单位）
            this.timerForGame.Interval = 1000;
        }

        private void GameArea_Paint(object sender, PaintEventArgs e)
        {
            if (_game != null)
            {
                _game.Draw(e.Graphics, this.GameArea.Size);

                //在游戏区域下方，设置走棋提示
                this.toolStripStatusLabel.Text = _game.NextIsBlack ? "请执黑子方走棋" : "请执白子方走棋";
            }
        }

        private void GameArea_MouseDown(object sender, MouseEventArgs e)
        {
            if (_game != null)
            {
                //调用鼠标点击处理程序
                _game.MouseDown(e.Location, this.GameArea.Size);
                //重绘游戏区域以反映变化（否则仅在Game里变化)
                this.GameArea.Invalidate();

                //游戏结束与谁胜判断
                if (_game.IsGameOver()==0)
                {
                    MessageBox.Show("游戏结束执黑子方胜");
                    _game = new Game();
                }
                if (_game.IsGameOver() == 1)
                {
                    MessageBox.Show("游戏结束执白子方胜");
                    _game = new Game();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //游戏计时
            currentCount += 1;
            //写入
            this.txt_ShowTime.Text = currentCount.ToString().Trim();
        }

        //点按“开始游戏”后
        private void btnStartGame_Click(object sender, EventArgs e)
        {
            //重置游戏
            _game = new Game();
            this.GameArea.Invalidate();
            //一旦玩家点按“开始游戏”按钮，Timer控件开始计时
            this.timerForGame.Start();
        }

        //点按“结束游戏”后
        private void btnGameOver_Click(object sender, EventArgs e)
        {
            Application.Exit();
            this.timerForGame.Stop();
            //一旦玩家点按“结束游戏”按钮，Timer控件停止计时
        }

        //“关于我”窗体
        private void btnaboutme_Click(object sender, EventArgs e)
        {
            FrmAboutme frm = new FrmAboutme();
            //关闭此窗体才可操作主游戏界面
            frm.ShowDialog();
        }

        //关于游戏规则窗体
        private void btnGuide_Click(object sender, EventArgs e)
        {
            FrmGuide frmGuide = new FrmGuide();
            frmGuide.ShowDialog();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
          //未用到
        }

        //播放背景音乐《安妮的仙境》
        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.AddExtension = true;
            openFileDialog.CheckFileExists = true;
            openFileDialog.CheckPathExists = true;
            //this nextsentence must be in single line
            openFileDialog.Filter = "MP3文件(*.mp3)|*.mp3";
            openFileDialog.DefaultExt = "*.mp3";
            if (openFileDialog.ShowDialog()==DialogResult.OK)
            {
                this.axWindowsMediaPlayer1.URL = openFileDialog.FileName;
            }
        }

        private void lblSecond_Click(object sender, EventArgs e)
        {
            //未用到
        }
    }
}
